package com.uniriho.lxr5002_service.internet;

import com.uniriho.lxr5002_service.been.RequestBean;
import com.uniriho.lxr5002_service.utils.LogUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by fyc on 2017/7/12.
 */

public class OkHttpUtil {
    public final static int READ_TIMEOUT = 30;
    public final static int CONNECT_TIMEOUT = 30;
    public final static int WRITE_TIMEOUT = 30;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private static final byte[] LOCKER = new byte[0];
    private static OkHttpUtil mInstance;
    private OkHttpClient mOkHttpClient;

    public OkHttpUtil() {
        okhttp3.OkHttpClient.Builder ClientBuilder = new okhttp3.OkHttpClient.Builder();
        ClientBuilder.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS);//读取超时
        ClientBuilder.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS);//连接超时
        ClientBuilder.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS);//写入超时
        //支持HTTPS请求，跳过证书验证
//        ClientBuilder.sslSocketFactory(createSSLSocketFactory());
        ClientBuilder.hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        mOkHttpClient = ClientBuilder.build();
    }

    /**
     * 单例模式获取OkHttpUtil
     *
     * @return
     */
    public static OkHttpUtil getInstance() {
        if (mInstance == null) {
            synchronized (LOCKER) {
                if (mInstance == null) {
                    mInstance = new OkHttpUtil();
                }
            }
        }
        return mInstance;
    }

    public String postBean(String url, RequestBean bean) {
        LogUtils.debug("requesturl", url);
        RequestBody formBody = new FormBody.Builder()
                .add("GPS_lng", bean.getGPS_lng() + "")
                .add("GPS_lat", bean.getGPS_lat() + "")
                .add("DevSerial", bean.getDevSerial())
                .add("PosNumber", bean.getPosNumber())
                .add("OtherData", bean.getOtherData())
                .build();

        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        LogUtils.debug("request", request.url().toString());
        Response response = null;
        try {
            response = mOkHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                assert response.body() != null;
                String responseStr = response.body().string();
                return responseStr;
            } else {
                LogUtils.debug("mOkHttpClient", "response fail");
                return null;
            }
        } catch (IOException e) {
            LogUtils.debug("mOkHttpClient", "request fail"+e.toString());
            return null;
        }
    }
}